<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
     String path=request.getContextPath();
     String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=basePath%>esayui/jquery.min.js"></script>
<script type="text/javascript" src="<%=basePath%>esayui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=basePath%>esayui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>esayui/themes/default/easyui.css" >
<link rel="stylesheet" type="text/css" href="<%=basePath%>esayui/themes/icon.css">
</head>
<body class="easyui-layout">
<div data-options="region:'north'" style="height:110px;">
欢迎${user.name}
<p>
<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-edit"  onclick="obj.editMenu()">修改权限</a>
<!--<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-edit"  onclick="obj.editMenu1()">删除权限</a> -->
<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-edit"  onclick="obj.editMenu2()">分配角色</a>
<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-add"  onclick="obj.editMenu3()">新建角色</a>
<p>
<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-no"  onclick="obj.logout()">退出登录</a>
</div>
   <div id="win" class="easyui-window"  style="width:600px;height:400px;"
      data-options="closed:true,title:'用户权限修改'">
      <from action="javascript:" method="post" id="menut">
      <div style="margin-left: 20px;margin-top: 20px">
                       你想为<input type="text" id="rolename" class="easyui-combobox" name="name" 
         data-options="valueField:'roleid',textField:'rolename',url:'role/findrole',prompt: '请选择角色',required:true,editable:false" />
                     <ul id="treemenu1"></ul>
      </div>
            <div style="margin-left: 20px;margin-top: 20px" align="center">
				<a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok"  onclick="obj.save()">确认</a>
			</div>                    
      </from>
    </div>
    
    <!--<div id="win1" class="easyui-window"  style="width:600px;height:400px;"
      data-options="closed:true,title:'用户权限修改'">
      <from action="javascript:" method="post" id="menut1">
      <div style="margin-left: 20px;margin-top: 20px">
                       你想为<input type="text" id="rolename1" class="easyui-combobox" name="name" 
         data-options="valueField:'roleid',textField:'rolename',url:'role/findrole',prompt: '请选择角色',required:true,editable:false" />
                角色删除什么权限：<select id="del" class="easyui-combobox" name="menuname" style="width:200px;">
                     <option value="1">删除管理</option>
                     <option value="2">修改管理</option>
                     <option value="3">增加管理</option>
                     </select>
       </div>
            <div style="margin-left: 20px;margin-top: 20px" align="center">
				<a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok"  onclick="obj.save1()">确认</a>
			</div>
                     
      </from>
    </div>-->
    <div id="win2" class="easyui-window"  style="width:600px;height:400px;"data-options="closed:true,title:'角色 修改'">
                      为<input type="text" id="name" class="easyui-combobox" name="name" onblur="yz()"
         data-options="valueField:'id',textField:'name',url:'user/selname',prompt: '请选择用户',required:true,editable:false" />
         分配为
         <!--<input type="text" id="rolename2" class="easyui-combobox" name="name" 
         data-options="valueField:'roleid',textField:'rolename',url:'role/findrole',prompt: '请选择角色',required:true,editable:false" />-->
         <input type="hidden" id="roleid">
         <div>
         <div id="div" style="width: 200px;height: 300px;float: left;">
             <table id="tab">
             </table>
             <div  id='tools' style="margin: 5px">
		<a href="javascript:" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="obj.add()">添加</a>
	</div>
         </div>
         <div id="div2" style="width:200px;height:300px;float: left;">
            <table id="tab2" border="1">
             <tr>
               <th>角色ID</th>
               <th>角色名</th>
             </tr>
            </table>
         </div>
         </div>
         <div style="margin-left: 20px;margin-top: 20px" align="center">
				<a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok"  onclick="obj.save2()">确认</a>
		 </div>
    </div>
    
    <div id="win3" class="easyui-window"  style="width:600px;height:400px;"data-options="closed:true,title:'新增角色'">
                新增角色：<input class="easyui-textbox" style="width:200px" name="rolename" id="rolename3" data-options="required:true">     
    <div style="margin-left: 20px;margin-top: 20px" align="center">
	     <a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok"  onclick="obj.save3()">确认</a>
    </div>
    </div>
    
<div data-options="region:'west'" style="width:150px;">
<input type="hidden" id="uid" value="${sessionScope.id}">
<ul id="menuTree"></ul>
</div>
<div data-options="region:'center'">
<div id="tabs" class="easyui-tabs" data-options="fit:true">
<div title="首页" style="padding:20px;display:none;">
<h2>欢迎使用</h2>
</div>
</div>
</div>
</body>
<script type="text/javascript">
obj={
		editMenu:function(){
			$("#win").window("open");
		},
		editMenu1:function(){
			$("#win1").window("open");
		},
		editMenu2:function(){
			$("#win2").window("open");
		},
		editMenu3:function(){
			$("#win3").window("open");
		},
		add:function(){
			var roleIds=[];
			var rolename="";
			var html1="";
			//获取选中行
			var rows= $('#tab').datagrid('getSelections');
			if(rows.length>0){
				$.each(rows,function(index,node){
					roleIds.push(node.roleid);	
				});
				 rolename=rows[0].rolename;
				 $.each(rows,function(index,item){
					 html1='<tr><td>'+item.roleid+'</td>'+'<td>'+item.rolename+'</td></tr>';
					 $("#tab2").append(html1);
				 });
			     $("#roleid").val(roleIds);
			}else{
				alert("请至少选择一行");
			}
		},
		save:function(){
			var role_id=$("#rolename").val();//11roleid
			var a=$('#treemenu1').tree('getChecked',['checked','indeterminate']);
			var ids=[];
			console.log(a);			
			$.each(a,function(index,node){
				if(node.pid==0){
				ids.push(node.id);	
				}
			});
			$.ajax({
				url:'user/update',
				type:'post',
				//传递数组需要进行序列化
				traditional:true,
				data:{
					"role_id":role_id,
					"ids":ids
				},
				success:function(data){
					
					$("#win").window('close');
					$.messager.show({
						title : "提示",
						msg	  : data ,
					})
				}
			});
		},
		save1:function(){
			var b=$('#del option:selected').val();
			var roleid=$('#rolename1').val();
			/*$.ajax({
				url:'user/deldate',
				type:'post',
				data:{
					"role_id":role_id,
					"menu_name":b
				},
				success:function(data){
					$("#win1").window('close');
					$.messager.show({
						title : "提示",
						msg	  : data ,
					})
				}
			});*/
		},
		save2:function(){
			//完成
			var id=$("#name").val();
			var roleIds=$("#roleid").val();
            console.log(roleIds);
			alert(id);//用户id
			//alert(roleId);//11roulid
			if(roleIds.length>0){
			$.ajax({
				url:'user/uprole',
				type:'post',
				data:{
					'id':id,
					'roleids':roleIds
				},
				success:function(data){
					$("#win2").window('close');
					$.messager.show({
						title : "提示",
						msg	  : data ,
					});
					setTimeout(function () {
                        window.location.reload();
                    }, 2000);
				}
			});
			}else{
				alert("请先添加角色");
			}
		},
		save3:function(){
			var rolename=$("#rolename3").val();
			if(rolename!=''){	
			$.ajax({
				url:'role/addrolename',
				type:'post',
				data:{
					'rolename':rolename
				},
				success:function(data){
					$("#win3").window('close');
					$.messager.show({
						title : "提示",
						msg	  : data ,
					});
					setTimeout(function () {
                        window.location.reload();
                    }, 2000);
				}
			})
			}else{
				$.messager.alert({
					title : "提示",
					msg	  : "输入为空，请重新输入" ,
					icon:"error",
				});
			};
		},
		logout:function(){
			$.ajax({
				url:'user/logout',
				type:'post',
				success:function(data){
					$.messager.show({
						title : "提示",
						msg	  : data,
					});
					setTimeout(function () {
						window.location.href='login.jsp';
                    }, 800);
				}
			});
		}
};
$(function(){
	$("input",$("#name").next("span")).blur(function(){
		var id=$("#name").val();
		alert(id);
		$.ajax({
			url:'role/findrolebyid',
			type:'post',
			data:{
				'id':id
			},
			success:function(data){
				appender(data);
				console.log(data);
			}
		})
	});
	function appender(data){
		var html='';
		$.each(data,function(index,item){
	     html=
	    	 '<tr><td>'+item.roleid+'</td>'+'<td>'+item.rolename+'</td></tr>'
	    $("#tab2").append(html);
		})
	};
	//获取角色id
	var uid=$("#uid").val();
	$('#menuTree').tree({
		method : 'post',
		url : 'menu/query?id=0&uid='+uid,
		onClick:function(node){
			var url=node.url;
			var title=node.text;			
			var content="<iframe frameborder='0' scrolling='auto' style='width:100%;height:100%;' src="+url+ "></iframe>" ;
			var isExist = $("#tabs").tabs("exists", title);
			                    if (isExist == false) {
			                        $("#tabs").tabs("add", {
			                            title: title,
			                            closable:true,
			                            content:content
			                        });
			                    }
			                    else{
			                        $("#tabs").tabs("select", title);
			                    }
		}
	});
	$('#treemenu1').tree({
		url:'menu/query?id=0&uid=3',
		method:'post',
		checkbox:true,
	});
	$('#tab').datagrid({
		url:'role/findrole',
		 columns:[[{
			 field:'roleid',
			 title:'id',
			 width:100,
			 align : 'center',
		     checkbox : true ,
		 },{
			 field:'rolename',
			 title:'角色',
			 width:100,
			 align : 'center',
		 }]],
	     toolbar: "#tools",
	})
})
</script>
</html>